// var mUrl = 'http://47.93.1.92:8999/hunt-admin';
//var mUrl = 'http://192.168.8.103:8080';
//var mUrl = 'http://39.105.51.148:6060/hunt-admin';
//var mUrl = 'http://39.105.51.148:7878/hunt-admin';
var mUrl = 'http://39.105.51.148:7878/hunt-admin';//正式服务器
//var mUrl = 'http://192.168.1.152:8080/hunt-admin';//测试服务器
//var mUrl = 'http://localhost:8080';//本机服务器
user_tool_accountforcustomer = {
    form_clear: function () {
        $("#accountforcustomer_form").form('reset');
        $("#accountforcustomer_form").form('clear');
        $("#accountforcustomer_grid").treegrid("uncheckAll");
    },
    search_form_clear: function () {
        $("#accountforcustomer-search-form").form('reset');
        $("#accountforcustomer-search-form").form('clear');
    },
    //生成查询页面
    init_main_view: function () {
        var userid = $("input[name='accountforcustomer-search-loginName']").val();
        var m_username = $("input[name='accountforcustomer-search-username']").val();
        var phone = $("input[name='accountforcustomer-search-phone']").val();
        var inselectedloginName = $('#inselectedloginName').text();
        console.log(inselectedloginName);
        $("#accountforcustomer_grid").datagrid({
            url: getRootPath()+'/account/listforcustomer'+ '?inselectedloginName=' + encodeURIComponent(inselectedloginName),
            showHeader:true,
            method: 'get',
            idField: "id",
            fitColumns: true,
            toolbar: '#accountforcustomer-tool-bar',
            rownumbers: true,
            animate: true,
            singleSelect: true,
            fit: true,
            border: false,
            pagination: true,
            striped: true,
            pagePosition: "bottom",
            pageNumber: 1,
            pageSize: 15,
            pageList: [15, 30, 45, 60],
            queryParams: {
                userid: userid,
                username: m_username,
                phone: phone
            },
            columns: [[
                {title: "选择", field: "ck", checkbox: true},
                {title: "中文名", field: "username" },
                {title: "登录账号", field: "userid"},
                {title: "登录密码", field: "pwd", width: 124},
                // {title: "指标权限", field: "userlevel", width: 150},
                {
                    title: "指标权限", width: 150, field: "userlevel", formatter: function (value, row, index) {
                        if (value == 0) {
                            return "体验用户";
                        }
                        if (value == 1) {
                            return "租用账户";
                        }
                        if (value == 2) {
                            return "经典账户";
                        }
                        if (value == 3) {
                            return "尊享账户";
                        }
                        if (value == 4) {
                            return "标准账户";
                        }
                        if (value == 5) {
                            return "待定账户1";
                        }
                        if (value == 6) {
                            return "待定账户2";
                        }
                    }
                },
                {
                    title: "在线状态", width: 150, field: "viewgpmax", formatter: function (value, row, index) {
                        if (value >= 2) {
                            return "在线";
                        }
                        else{
                            return "离线";
                        }

                    },
                    styler: function (value, row, index) {
                        if(value== 2){  //不需要条件可不写
                            return 'font-weight:bold;color:red';
                        }
                    }
                },
                {
                    title: "性别", width: 100, field: "sex", formatter: function (value, row, index) {
                        if (value == 1) {
                            return "男";
                        }
                        if (value == 2) {
                            return "女";
                        }
                    }
                },
                {title: "服务器", field: "serverid", width: 100},
                {title: "电话", field: "phone", width: 300},
                {title: "地址", field: "address", width: 300},
                {title: "开户时间", field: "kaihutime", width: 100},
                {title: "到期时间", field: "validdate", width: 100},
                {title: "资金量", field: "money", width: 150,　formatter:function(data){return data/10000+'万'}},
                {title: "身份证号", field: "identitycard", width: 300},
                {title: "年龄", field: "age", width: 100},
                {title: "在线时长(小时)", field: "usedtotaltime", width: 100},
                {title: "开号路径", field: "channel", width: 100,formatter:function(data){
                        if(data=='0'){
                            return '官网录入'
                        }
                        else if(data=='1'){
                            return '开号系统录入'
                        }
                        else if(data=='2'){
                            return '一键登录生成'
                        }
                        else {
                            return '未知'
                        }
                    }},
                {title: "地址id", field: "addressid", width: 0,hidden:true},



            ]],
            onLoadSuccess: function (data) {
                $(".datagrid-header-check").html("");
                $(".status").switchbutton({
                    readonly: true,
                    onText: '已启用',
                    offText: '已禁用',
                    width: 80,
                })
            },
            onLoadError:function(){

            },

        });
    },
    //生成添加修改界面
    init_edit_view: function (type) {
        $("#accountforcustomer_edit_dialog").dialog({
            title: '新增用户',
            iconCls: 'icon-save',
            closable: true,
            width: 500,
            height: 500,
            cache: false,
            modal: true,
            resizable: true,
            'onBeforeOpen': function () {
                // 修改
                if(type ==2){
                    $('#accountforcustomer_edit_dialog').panel({title: "修改用户信息"});
                    $('#userid').textbox('textbox').attr('readonly',true);


                }else{
                    $('#accountforcustomer_edit_dialog').panel({title: "新增用户"});
                    $('#userid').textbox('textbox').attr('readonly',false);
                }


            },
            'onOpen': function () {
                $('#userlevel').combobox({
                    onChange:function (){
                        $('#exp').textbox('setValue','');
                    }
                })
                $('#identitycard').textbox({
                    onChange: function(value) {
                        let result=0;
                        let today=new Date()
                        let x=  $('#identitycard').textbox('getValue')
                        let countyear=parseInt(x.slice(6, 10))
                        let countmonth=parseInt(x.slice(10, 12))
                        let countday=parseInt(x.slice(12, 14))
                        result = today.getFullYear() - countyear;
                        if (countmonth - today.getMonth() == 1) {
                            if (countday - today.getDate() > 0) {
                                result--
                            }
                        }
                        if (countmonth - today.getMonth() >1) {
                            result--
                        }
                        $('#age').textbox('setValue',result)
                        // 根据身份证计算年龄
                    }
                });
                $('#a1forcustomer_address').combobox({
                    valueField: 'id',
                    textField: 'area',
                    method:'get',
                    url: getRootPath()+'/account/getarea',
                    onSelect: function(rec){
                        $('#a2forcustomer_address').combobox('clear');
                        var url = getRootPath()+'/account/getarea?code='+rec.id;
                        $('#a2forcustomer_address').combobox('reload', url);
                    }
                });
                $('#a2forcustomer_address').combobox({
                    valueField: 'id',
                    textField: 'area',
                    method:'get'
                });
                if(type ==2){
                    var users = $("#accountforcustomer_grid").datagrid('getChecked');
                    var addressid=users[0].addressid
                    if(addressid%10000==0){
                        $('#a1forcustomer_address').combobox('setValue',addressid)
                    }
                    else {
                        $('#a1forcustomer_address').combobox('setValue',Math.floor(addressid/10000)*10000)
                        setTimeout(function(){
                            $('#a2forcustomer_address').combobox('setValue',addressid)
                        },100)

                    }
                    $('#userlevel').combobox('setValue',users[0].userlevel)
                }
            },
            'onClose': function () {
                user_tool_accountforcustomer.form_clear();
            },
            buttons: [
                {
                    text: '保存',
                    width: 100,
                    iconCls: 'icon-save',
                    handler: function () {
                        if (type == 1) {
                            user_tool_accountforcustomer.save();
                        }
                        if (type == 2) {
                            user_tool_accountforcustomer.updateAccount();
                        }
                    }
                },
                {
                    text: '清除',
                    width: 100,
                    iconCls: 'icon-reload',
                    handler: function () {
                        user_tool_accountforcustomer.form_clear();
                    }
                },
                {
                    text: '取消',
                    width: 100,
                    iconCls: 'icon-add',
                    handler: function () {
                        user_tool_accountforcustomer.form_clear();
                        $("#account_edit_dialog").dialog('close');
                    }
                }
            ],
        })
    },
    save: function () {
        var form_isValid = $("#accountforcustomer_form").form('validate');
        var phone = $('#accountforcustomer_edit_dialog input[id="phone"]').val();
        if (!form_isValid) {
            common_tool.messager_show("请输入正确信息")
        } else if(!validatePhoneVal(phone)) { //再次判断手机号码
            common_tool.messager_show("请输入正确的手机号码")
        }
        else {
            var loginName =  $('#loginName').text();
            var userid = $('#accountforcustomer_edit_dialog input[id="userid"]').val();
            var username = $('#accountforcustomer_edit_dialog input[id="username"]').val();
            var pwd = $('#accountforcustomer_edit_dialog input[id="pwd"]').val();
            var sex = $('#accountforcustomer_edit_dialog select[id="sex"]').combobox('getValue');
            var money = $('#accountforcustomer_edit_dialog input[id="money"]').val();
            var identitycard = $('#accountforcustomer_edit_dialog input[id="identitycard"]').val();
            var age = $('#accountforcustomer_edit_dialog input[id="age"]').val();
            var exp = $('#accountforcustomer_edit_dialog input[id="exp"]').val();
            console.log(loginName);
            if($('#a2forcustomer_address').combobox('getValue')!==""){
                var addressid=$('#a2forcustomer_address').combobox('getValue')
            }
            else {
                var addressid=$('#a1forcustomer_address').combobox('getValue')
            }
            var userlevel = $('#accountforcustomer_edit_dialog select[id="userlevel"]').combobox('getValue');
            var address=$('#a1forcustomer_address').combobox('getText')+$('#a2forcustomer_address').combobox('getText')

            //用户等级容错处理 特定情况会显示text的值 导致后台报错
            if("体验账户" == userlevel) {
                userlevel = 0;
            } else if("租用账户" == userlevel) {
                userlevel = 1;
            } else if("经典账户" == userlevel) {
                userlevel = 2;
            } else if("尊享账户" == userlevel) {
                userlevel = 3;
            } else if("标准账户" == userlevel) {
                userlevel = 4;
            }else if("待定账户1" == userlevel) {
                userlevel = 5;
            }else if("待定账户2" == userlevel) {
                userlevel = 6;
            }

            $.ajax({

                data: {
                    userid: userid,
                    username: username,
                    pwd: pwd,
                    sex: sex,
                    money: money*10000,
                    phone: phone,
                    addressid: addressid,
                    userlevel:userlevel,
                    identitycard:identitycard,
                    age:age,
                    address:address,
                    exp:exp,
                    loginName:loginName

                },
                traditional: true,
                method: 'post',
                url: getRootPath()+'/account/insertAccountforcustomer',
                // url: getRootPath() + '/user/insertAccount',
                async: false,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 10000) {
                        $("#accountforcustomer_edit_dialog").dialog("close");
                        user_tool_accountforcustomer.form_clear();
                        user_tool_accountforcustomer.init_main_view();
                        common_tool.messager_show("新用户开户成功");
                        return false;
                    }
                    else {
                        common_tool.messager_show(result.msg);
                    }
                },
            });

        }

    },
    updateAccount: function (data) {
        var form_isValid = $("#accountforcustomer_form").form('validate');
        if (!form_isValid) {
            common_tool.messager_show("请输入正确信息")
        }
        else {
            var loginName =  $('#loginName').text();
            var userid = $('#accountforcustomer_edit_dialog input[id="userid"]').val();
            var username = $('#accountforcustomer_edit_dialog input[id="username"]').val();
            var pwd = $('#accountforcustomer_edit_dialog input[id="pwd"]').val();
            var sex = $('#accountforcustomer_edit_dialog select[id="sex"]').combobox('getValue');
            var money = $('#accountforcustomer_edit_dialog input[id="money"]').val();
            var phone = $('#accountforcustomer_edit_dialog input[id="phone"]').val();
            var address=$('#a1forcustomer_address').combobox('getText')+$('#a2forcustomer_address').combobox('getText')
            var userlevel = $('#accountforcustomer_edit_dialog select[id="userlevel"]').combobox('getValue');
            var identitycard = $('#accountforcustomer_edit_dialog input[id="identitycard"]').val();
            var age = $('#accountforcustomer_edit_dialog input[id="age"]').val();
            var exp = $('#accountforcustomer_edit_dialog input[id="exp"]').val();
            if($('#a2forcustomer_address').combobox('getValue')!==""){
                var addressid=$('#a2forcustomer_address').combobox('getValue')
            }
            else {
                var addressid=$('#a1forcustomer_address').combobox('getValue')
            }
            $.ajax({
                data: {
                    userid: userid,
                    username: username,
                    pwd: pwd,
                    sex: sex,
                    money: money*10000,
                    phone: phone,
                    address: address,
                    userlevel:userlevel,
                    identitycard:identitycard,
                    age:age,
                    addressid:addressid,
                    exp:exp,
                    loginName:loginName
                },
                traditional: true,
                method: 'post',
                url: getRootPath()+'/account/updateAccountforcustomer',
                async: false,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 10000) {
                        $("#accountforcustomer_edit_dialog").dialog("close");
                        user_tool_accountforcustomer.form_clear();
                        user_tool_accountforcustomer.init_main_view();
                        common_tool.messager_show(result.msg);
                        return false;
                    }
                    else {
                        common_tool.messager_show(result.msg);
                    }
                },
            });

        }
    },
    //负责地址栏下拉框工作
    address:function(){
        $('#add1').combobox({
            valueField: 'id',
            textField: 'area',
            method:'get',
            url: getRootPath()+'/account/getarea',
            onSelect: function (rec) {
                $("input[name='account-search-address']").val(rec.area)
                $('#add2').combobox('clear');
                var url = getRootPath()+'/account/getarea?code='+rec.id;
                $('#add2').combobox('reload', url);
            }
        });
        $('#add2').combobox({
            valueField: 'id',
            textField: 'area',
            method:'get'
        });

    },
    delete: function (id) {
        var loginName =  $('#loginName').text();
        $.ajax({
            data: {
                userid: id,
                loginName:loginName
            },
            traditional: true,
            method: 'get',
            url: getRootPath()+'/account/deleteAccountforcustomer',
            async: false,
            dataType: 'json',
            success: function (result) {
                if (result.code == 10000) {
                    user_tool_accountforcustomer.form_clear();
                    user_tool_accountforcustomer.init_main_view();
                    common_tool.messager_show(result.msg);
                    return false;
                }
                else {
                    common_tool.messager_show(result.msg);
                }
            },
        });
    },

};
$(document).ready(function () {
    // user_tool_accountforcustomer.init_start_view()
    user_tool_accountforcustomer.address();
    $("#accountforcustomer-save-btn").click(function () {
        user_tool_accountforcustomer.init_edit_view(1);
    });
    $("#accountforcustomer-update-btn").click(function () {
        var users = $("#accountforcustomer_grid").datagrid('getChecked');
        if (users.length == 0) {
            common_tool.messager_show("请您至少选择一条记录");
            return false;
        }
        $("#accountforcustomer_form").form('load', {

            username: users[0].username,
            userid: users[0].userid,
            pwd: users[0].pwd,
            sex: users[0].sex,
            birth: users[0].birth,
            money: users[0].money/10000,
            phone: users[0].phone,
            identitycard: users[0].identitycard,
            userlevel: users[0].userlevel,
            age:users[0].age
        });
        user_tool_accountforcustomer.init_edit_view(2);
    });
    $("#accountforcustomer-delete-btn").click(function () {
        var users = $("#accountforcustomer_grid").datagrid('getChecked');
        if (users.length == 0) {
            common_tool.messager_show("请您至少选择一条记录");
            return false;
        }
        $.messager.confirm('确认对话框', "您确认删除"+users[0].userid+"该账户吗?", function (r) {
            if (r) {
                user_tool_accountforcustomer.delete(users[0].userid);
            }
        });
    });
    $("#accountforcustomer-detail-btn").click(function () {

    });
    $("#useraccountforcustomer-enable-btn").click(function () {
        var users = $("#accountforcustomer_grid").datagrid('getChecked');
        if (users.length == 0) {
            common_tool.messager_show("请至少选择一条记录");
            return false;
        }
        if (users[0].status == 1) {

            common_tool.messager_show("该账号已经处于启用状态");
            return false;
        }
        $.messager.confirm('确认对话框', "您确认启用 " + users[0].username + " 账号吗?", function (r) {
            if (r) {
                user_tool_accountforcustomer.enableUser(users[0].id)
            }
        });
    });
    $("#accountforcustomer-forbidden-btn").click(function () {
        var users = $("#accountforcustomer_grid").datagrid('getChecked');
        if (users.length == 0) {
            common_tool.messager_show("请至少选择一条记录");
            return false;
        }
        if (users[0].status == 3) {
            common_tool.messager_show("该账号已经处于禁用状态");
            return false;
        }
        $.messager.confirm('确认对话框', "您确认禁用 " + users[0].username + " 账号吗?", function (r) {
            if (r) {
                user_tool_accountforcustomer.forbiddenUser(users[0].id)
            }
        });
    });
    $("#accountforcustomer-flash-btn").click(function () {
        user_tool_accountforcustomer.form_clear();
        user_tool_accountforcustomer.init_main_view();
    });
    $("#accountforcustomer-select-btn").click(function () {
        user_tool_accountforcustomer.init_main_view();
    });
    $("#accountforcustomer-password-btn").click(function () {
        var users = $("#accountforcustomer_grid").datagrid('getChecked');
        if (users.length == 0) {
            common_tool.messager_show("请至少选择一条记录");
            return false;
        }
        user_tool_accountforcustomer.password_view(users[0]);
    });
    $("#accountforcustomer-clear-btn").click(function () {
        user_tool_accountforcustomer.search_form_clear()
    });

});


//单独电话校验方法 提交时候需要再次校验
function validatePhoneVal(value) {
    return (value && /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[0-9]{1})|(19[0-9]{1})|(18[0-9]{1}))+\d{8})$/.test(value));
}